'''
Created on 2016-8-11

@author: lvchengfeng
'''
#!/usr/bin/python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup, Tag
html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title1"><b>The Dormouse's story11</b></p>
<p class="title"><b>The Dormouse's story</b></p>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://www.jb51.net" class="sister" id="link1">Elsie</a>,
<a href="http://www.jb51.net" class="sister" id="link2">Lacie</a> and
<a href="http://www.jb51.net" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">...</p>
"""
soup = BeautifulSoup(html_doc)
print soup.title
print soup.title.name
print soup.title.string
print soup.p
print soup.a
print soup.find_all('a')
print soup.find(id='link3')
print soup.get_text()
print "================================"
soup = BeautifulSoup(open('test.html'))
#print soup.prettify()
soup.p['class']="title1"
print soup.p
print soup.p.string
print soup.head.contents
print soup.select('title')
print soup.select('.title')
print "================================"
fobj = open('test.html');
data = fobj.read();
fobj.close();
soup = BeautifulSoup(data);
spantest1 = soup.find(id='test1')
spantest1.insert(0, "dasds")
print str(soup.textarea).replace('sdasd','new')

original_tag = soup.td;
new_tag = soup.new_tag('a', href='http://www.example.com')
new_tag.insert(0, 'Text!')
original_tag.append(new_tag)

trlist = soup.findAll("tr");
rownum = len(trlist);
colnum = len(trlist[0]);
print colnum;
trlist[0].insert(colnum, "<td>hahaha</td>");
tdlist = trlist[0].findAll("td");
#content= tdlist[0].renderContents();
Fobj = file("test1.html", "w");
Fobj.write(str(soup));
Fobj.close();

'''
soup = BeautifulSoup()
mem_attr = ['Description', 'PhysicalID', 'Slot', 'Size', 'Width']
html = Tag(soup,'',"html")
table = Tag(soup, "table")
tr = Tag(soup, "tr")
soup.append(html)
html.append(table)
table.append(tr)
for attr in mem_attr:
    th = Tag(soup, "th")
    tr.append(th)
    th.append(attr)

print soup.prettify()
'''